package com.ruoyi.database.config;


import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;


public class DataSourceRouting extends AbstractRoutingDataSource {

    private final Map<Object, DataSource> targetDataSources = new HashMap<>();

    @Override
    protected Object determineCurrentLookupKey() {

        return DataSourceContextHolder.getDataSourceType();
    }

    //添加到数据源
    public void addDataSource(String key, DataSource dataSource) {

        targetDataSources.put(key, dataSource);
        setTargetDataSources(new HashMap<>(targetDataSources)); // 复制一份到 AbstractRoutingDataSource
        afterPropertiesSet(); // 确保数据源更新生效
    }



}